
package Individual;

import Settings.Settings;

public class CrossoverFunctions {

    public static void crossover( Individual[] individuals) {

        Individual individual = null;

        for(int i = 0; i < individuals.length; ++i)
            if(Math.random() < Settings.PROPABILITY_CROSSOVER)
                if(individual == null) {

                    individual = individuals[i];

                } else {

                    switch(Settings.CROSSOVER_FUNCTION_TYPE) {
                        case 0:
                            crossoverOnePoint(individual, individuals[i]);
                            break;
                    }

                    individual = null;

                }        
       
    }

    private static void crossoverOnePoint( Individual individual1,
                                           Individual individual2) {

        int cutPosition =  (int) ( (double) ( individual1.numOfClusters *
                                              individual1.numOfFeatures *
                                              individual1.numOfBitsPerValue ) *
                                            Math.random()
                                 );

        int cluster = cutPosition / ( individual1.numOfFeatures *
                                      individual1.numOfBitsPerValue
                                    );

        int feature = cutPosition % ( individual1.numOfFeatures *
                                      individual1.numOfBitsPerValue
                                    );

        int bit = feature % individual1.numOfBitsPerValue;

        feature /= individual1.numOfBitsPerValue;

        for(int c = cluster; c < individual1.numOfClusters; ++c) {
            for(int f = feature; f < individual1.numOfFeatures; ++f) {
                for(int b = bit; b < individual1.numOfBitsPerValue; ++b) {

                    boolean v1 = individual1.chromosome.get(c).get(f).get(b);
                    boolean v2 = individual2.chromosome.get(c).get(f).get(b);

                    individual1.chromosome.get(c).get(f).set(b, v2);
                    individual2.chromosome.get(c).get(f).set(b, v1);

                }

                bit = 0;

            }

            feature = 0;

        }

    }

}
